//
//  ExDynamicViewController.m
//  OC-Example
//
//  Created by apang on 2018/7/31.
//  Copyright © 2018年 apang.men All rights reserved.
//

#import "ExDynamicViewController.h"

@interface ExDynamicViewController ()<UICollisionBehaviorDelegate>
@property (strong, nonatomic) UIDynamicAnimator * animator;
@property (strong, nonatomic) UIView * aView;
@property (strong, nonatomic) UIDynamicItemBehavior * dynamicItem;
@property (assign, nonatomic) CGPoint centern;
@end

@implementation ExDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"重力模拟";
    [self.view setBackgroundColor:[UIColor whiteColor]];

    self.aView = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-100.0)/2.0, 0, 100, 100)];
    [self.aView setBackgroundColor:[UIColor grayColor]];
    [self.aView.layer setCornerRadius:50.0];
    [self.aView.layer setMasksToBounds:YES];
    [self.view addSubview:self.aView];


    UIDynamicAnimator * animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    UIGravityBehavior * gravityBe = [[UIGravityBehavior alloc] initWithItems:@[self.aView]];
    UICollisionBehavior * collisionBe = [[UICollisionBehavior alloc] initWithItems:@[self.aView]];
    collisionBe.translatesReferenceBoundsIntoBoundary = YES;
    self.dynamicItem = [[UIDynamicItemBehavior alloc] initWithItems:@[self.aView]];
    self.dynamicItem.elasticity = 0.6;
    [animator addBehavior:self.dynamicItem];
    [animator addBehavior:gravityBe];
    [animator addBehavior:collisionBe];
    self.animator = animator;
    self.centern = self.aView.center;

    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTranslatesAutoresizingMaskIntoConstraints:NO];

    [button setTitle:@"掉落" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(animatorUpdate:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button]-(0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[button]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]];
}

- (void)animatorUpdate:(id)sender {
    [self.dynamicItem addLinearVelocity:CGPointMake(0, -1*[self.dynamicItem linearVelocityForItem:self.aView].y) forItem:self.aView];
    self.aView.center = self.centern;
    [self.animator updateItemUsingCurrentState:self.aView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
